package com.tywj.panda.user.order

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.core.widget.recycleview.adapter.BaseViewHolder
import com.tywj.panda.customer.common.constant.GlobalCode
import com.tywj.panda.customer.common.tool.MoneyTool
import com.tywj.panda.customer.common.widget.recyclerview.MyBaseAdapter
import com.tywj.panda.user.R
import com.tywj.panda.user.entity.UserOrderEntity


/**
 * @desc 我的订单适配器
 * @author June Yang
 * @date 2020/5/13
 * @version 2.0.0
 */
class UserOrderRecordAdapter :
    MyBaseAdapter<UserOrderEntity.DataBean>(R.layout.user_recycle_item_order_record) {

    override fun convert(helper: BaseViewHolder, item: UserOrderEntity.DataBean) {
        super.convert(helper, item)

        helper.setText(R.id.tvLineName, item.lineName)
        helper.setText(
            R.id.tvOrderTime,
            String.format(ResourcesUtil.getString(R.string.user_order_create_time), item.orderTime)
        )

        helper.setText(R.id.tvStatus, GlobalCode.getOrderStatus(item.orderStatus))

        helper.setText(R.id.tvOrderNo, String.format(ResourcesUtil.getString(R.string.user_order_no), item.orderNo))

        helper.setText(
            R.id.tvOrderPrice,
            String.format(ResourcesUtil.getString(R.string.user_order_total_price), MoneyTool.formatFen(item.orderFee))
        )
    }
    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {

                val position = parent.getChildLayoutPosition(view)
                if (position != RecyclerView.NO_POSITION) {
                    when (position) {
                        // 第一个
                        0 -> {
                            outRect.top = ResourcesUtil.getDimensionPixelSize(R.dimen.dp_10)
                            outRect.bottom = ResourcesUtil.getDimensionPixelSize(R.dimen.dp_10)
                        }
                        // footer
                        this@UserOrderRecordAdapter.data.size + this@UserOrderRecordAdapter.headerLayoutCount -> outRect.top = 0
                        // 其他
                        else -> outRect.bottom = ResourcesUtil.getDimensionPixelSize(R.dimen.dp_10)
                    }
                }
            }
        })
    }
}